import signinTpl from "../views/signin.art";

const signinHtml=signinTpl({});

//登陆页面点击登录按钮事件监听
const _handleSubmit=(router)=>{
    return (e)=>{
        e.preventDefault();
        let data=$("#signin").serialize();
        $.ajax({
            url:"/api/users/login",
            type:"post",
            headers:{
                "X-Access-Token":localStorage.getItem("lg-token")||""
            },
            data,
            success(res,textStatus,jqXHR){
                // console.log(jqXHR.getResponseHeader("X-Access-Token"));
                localStorage.setItem("lg-token",jqXHR.getResponseHeader("X-Access-Token"));
                router.go("/index");
            },
            error(err){
                console.log("用户名或密码错误",err);
            }
        });
    };
};

//渲染登录页面
const login=(router)=>{
    return (req,res,next)=>{
        res.render(signinHtml);
        $("#signin").on("submit",_handleSubmit(router));
    };
};
export default login